package gui;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Process;
import model.ProcessType;
import service.Service;

/**
 * 
 * @author Ceslovas Lopan
 * 
 */
public class AddEditProcess extends JFrame {
	private final Controller controller = new Controller();
	private Component owner;
	private JTextField txfName, txfMinH, txfMinM, txfIdealH, txfIdealM,
			txfMaxH, txfMaxM;
	private JCheckBox chckbxRunsInStock;
	private JButton btnSave, btnCancel;
	private Process process = null;

	public AddEditProcess(Component owner, Process proc) {
		this(owner);
		process = proc;
		fillData();
	}

	public AddEditProcess(Component owner) {
		this.owner = owner;
		setSize(239, 244);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLocation(100, 100);
		setResizable(false);
		setTitle("Add/Edit process");
		getContentPane().setLayout(new GridLayout(6, 0, 0, 0));

		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		getContentPane().add(panel);

		JLabel lblName = new JLabel("Name");
		panel.add(lblName);

		txfName = new JTextField();
		panel.add(txfName);
		txfName.setColumns(10);

		JPanel panel1 = new JPanel();
		FlowLayout flowLayout1 = (FlowLayout) panel1.getLayout();
		flowLayout1.setAlignment(FlowLayout.LEFT);
		getContentPane().add(panel1);

		JLabel lblMinimum = new JLabel("Minimum");
		panel1.add(lblMinimum);

		txfMinH = new JTextField("0");
		panel1.add(txfMinH);
		txfMinH.setColumns(3);

		JLabel lblHours = new JLabel("hours");
		panel1.add(lblHours);

		txfMinM = new JTextField("0");
		panel1.add(txfMinM);
		txfMinM.setColumns(3);

		JLabel lblMinutes = new JLabel("minutes");
		panel1.add(lblMinutes);

		JPanel panel2 = new JPanel();
		FlowLayout flowLayout2 = (FlowLayout) panel2.getLayout();
		flowLayout2.setAlignment(FlowLayout.LEFT);
		getContentPane().add(panel2);

		JLabel lblIdeally = new JLabel("Ideally");
		panel2.add(lblIdeally);

		txfIdealH = new JTextField("0");
		panel2.add(txfIdealH);
		txfIdealH.setColumns(3);

		JLabel lblHours1 = new JLabel("hours");
		panel2.add(lblHours1);

		txfIdealM = new JTextField("0");
		panel2.add(txfIdealM);
		txfIdealM.setColumns(3);

		JLabel lblMinutes1 = new JLabel("minutes");
		panel2.add(lblMinutes1);

		JPanel panel3 = new JPanel();
		FlowLayout flowLayout3 = (FlowLayout) panel3.getLayout();
		flowLayout3.setAlignment(FlowLayout.LEFT);
		getContentPane().add(panel3);

		JLabel lblMaximum = new JLabel("Maximum");
		panel3.add(lblMaximum);

		txfMaxH = new JTextField("0");
		panel3.add(txfMaxH);
		txfMaxH.setColumns(3);

		JLabel lblHours2 = new JLabel("hours");
		panel3.add(lblHours2);

		txfMaxM = new JTextField("0");
		panel3.add(txfMaxM);
		txfMaxM.setColumns(3);

		JLabel lblMinutes2 = new JLabel("minutes");
		panel3.add(lblMinutes2);

		JPanel panel4 = new JPanel();
		FlowLayout flowLayout4 = (FlowLayout) panel4.getLayout();
		flowLayout4.setAlignment(FlowLayout.LEFT);
		getContentPane().add(panel4);

		chckbxRunsInStock = new JCheckBox("Runs in stock area");
		panel4.add(chckbxRunsInStock);

		JPanel panel5 = new JPanel();
		FlowLayout flowLayout5 = (FlowLayout) panel5.getLayout();
		flowLayout5.setAlignment(FlowLayout.RIGHT);
		getContentPane().add(panel5);

		btnSave = new JButton("Save");
		panel5.add(btnSave);

		btnCancel = new JButton("Cancel");
		panel5.add(btnCancel);

		btnSave.addActionListener(controller);
		btnCancel.addActionListener(controller);
	}

	public void fillData() {
		txfName.setText(process.getName());

		txfMinH.setText(Service.getInstance()
				.milisToHours(process.getMinTime()) + "");
		txfMinM.setText(Service.getInstance().milisToMins(process.getMinTime())
				+ "");

		txfIdealH.setText(Service.getInstance().milisToHours(
				process.getIdealTime())
				+ "");
		txfIdealM.setText(Service.getInstance().milisToMins(
				process.getIdealTime())
				+ "");

		txfMaxH.setText(Service.getInstance()
				.milisToHours(process.getMaxTime()) + "");
		txfMaxM.setText(Service.getInstance().milisToMins(process.getMaxTime())
				+ "");

		chckbxRunsInStock
				.setSelected(process.getProcessType() == ProcessType.IN_STOCK);
	}

	private class Controller implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == btnSave) {
				try {
					String name = txfName.getText();
					int minH = Integer.parseInt(txfMinH.getText());
					int minM = Integer.parseInt(txfMinM.getText());
					int idealH = Integer.parseInt(txfIdealH.getText());
					int idealM = Integer.parseInt(txfIdealM.getText());
					int maxH = Integer.parseInt(txfMaxH.getText());
					int maxM = Integer.parseInt(txfMaxM.getText());

					ProcessType type = ProcessType.NOT_IN_STOCK;

					if (chckbxRunsInStock.isSelected()) {
						type = ProcessType.IN_STOCK;
					}

					long min = Service.getInstance().convertToMilis(minH, minM);
					long ideal = Service.getInstance().convertToMilis(idealH,
							idealM);
					long max = Service.getInstance().convertToMilis(maxH, maxM);

					Process p;

					if (process == null)
						p = Service.getInstance().addProcess(name, min, ideal,
								max, type);
					else
						p = Service.getInstance().editProcess(process, name,
								min, ideal, max, type);

					if (p != null) {
						AddEditProcess.this.dispose();
					} else {
						JOptionPane
								.showMessageDialog(
										AddEditProcess.this,
										"There was an error, process was not created/edited",
										"Error", JOptionPane.ERROR_MESSAGE);
					}
				} catch (NumberFormatException ex) {
					JOptionPane.showMessageDialog(AddEditProcess.this,
							"Please enter valid numbers", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
				if (owner instanceof ManageData) {
					((ManageData) owner).updateLists();
				}
			} else if (e.getSource() == btnCancel) {
				AddEditProcess.this.dispose();
			}
		}

	}
}
